package ar.com.fracervellera.web.controller;


import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.com.fracervellera.model.PMF;
import ar.com.fracervellera.server.repository.ProjectDAO;
import ar.com.fracervellera.server.repository.imp.ProjectDAOImp;
import ar.com.fracervellera.web.command.ProjectForm;
import ar.com.fracervellera.web.validator.ProjectValidator;
import ar.com.fracervellera.web.validator.ProjectValidatorImp;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

public class CreateProjectController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProjectValidator projectValidator = ProjectValidatorImp.getInstance();
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	private PersistenceManager pm ;
	private ProjectDAO projectDao = ProjectDAOImp.getInstance();
	private static final Logger log = Logger.getLogger("GooglePrivateClaendarService"); 
	
	@Override
	public void init() throws ServletException {
		super.init();
		pm = PMF.get().getPersistenceManager();
		
	}
	
	@Override
	public void destroy() {
		super.destroy();
		if(pm != null && !pm.isClosed()){
			pm.close();
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if(req.getSession().getAttribute("user") != null){
			req.getRequestDispatcher("/jsp/createProject.jsp").forward(req, resp);			
		}else{
			resp.sendRedirect("/login");
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			
		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
		if(blobs != null) {
			BlobKey blobKey = blobs.get("myProject");
			String title = "";
			String description = "";
			long date = 0;
			if (blobKey == null) {
				resp.sendRedirect("/");
			} else {
				log.warning("Se agrega un nuevo proyecto");
				title = req.getParameter("title");
				description = req.getParameter("description");
				date = new Date().getTime();
				ProjectForm form = new ProjectForm(title, description, blobKey, req.getContentType(),date);
				List<String> errors = new LinkedList<String>();
				if(projectValidator.isValid(form, errors) && req.getSession().getAttribute("user") != null){
					projectDao.create(form, pm);
					req.setAttribute("statusMsg", "El proyecto se cuardo correctamente");
				}else{
					req.setAttribute("errorList", errors);
				}
				resp.sendRedirect("/createProject");
				log.warning("Se creo correctamente el proyecto");
			}			
		}
	}
}
